package com.atguigu.edu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.Base.BaseStreamApp;
import com.atguigu.edu.realtime.bean.TradeCourseOrderBean;
import com.atguigu.edu.realtime.common.Constant;
import com.atguigu.edu.realtime.func.DimAsyncFunction;
import com.atguigu.edu.realtime.utils.AtguiguUtil;
import com.atguigu.edu.realtime.utils.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

/**
 * 统计当日下单人数、下单次数和下单金额、课程粒度
 * */
public class DwsTradeCourseOrderWindow extends BaseStreamApp {
    public static void main(String[] args) {
        new DwsTradeCourseOrderWindow().init(4001,2,"DwsTradeCourseOrderWindow", Constant.TOPIC_DWD_TRADE_ORDER_DETAIL);
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        //解析成pojo类型
        SingleOutputStreamOperator<TradeCourseOrderBean> beanStream = parseToPojo(stream);
        //分组，开窗，聚合
        SingleOutputStreamOperator<TradeCourseOrderBean> beanStreamWithoutDims = windowAndAgg(beanStream);
        //补充维度信息
        SingleOutputStreamOperator<TradeCourseOrderBean> beanStreamWithDims = joinDim(beanStreamWithoutDims);
        //写出到clickhouse
        beanStreamWithDims.addSink(FlinkSinkUtil.getClickHourseSink("dws_trade_course_order_window",TradeCourseOrderBean.class));
    }

    private SingleOutputStreamOperator<TradeCourseOrderBean> joinDim(SingleOutputStreamOperator<TradeCourseOrderBean> beanStreamWithoutDims) {
        //异步处理
        SingleOutputStreamOperator<TradeCourseOrderBean> courseInfoStream = AsyncDataStream.unorderedWait(
                beanStreamWithoutDims,
                new DimAsyncFunction<TradeCourseOrderBean>() {

                    @Override
                    public String getTable() {
                        return "dim_course_info";
                    }

                    @Override
                    public String getId(TradeCourseOrderBean input) {
                        return input.getCourseId();
                    }

                    @Override
                    public void addDim(TradeCourseOrderBean input, JSONObject dim) {
                        input.setSubjectId(dim.getString("SUBJECT_ID"));
                    }
                },
                //数据超时没处理完，则抛异步超时异常，数据忽略
                120,
                TimeUnit.SECONDS
        );
        SingleOutputStreamOperator<TradeCourseOrderBean> baseSubjectInfoStream = AsyncDataStream.unorderedWait(
                courseInfoStream,
                new DimAsyncFunction<TradeCourseOrderBean>() {
                    @Override
                    public String getTable() {
                        return "dim_base_subject_info";
                    }

                    @Override
                    public String getId(TradeCourseOrderBean input) {
                        return input.getSubjectId();
                    }

                    @Override
                    public void addDim(TradeCourseOrderBean input, JSONObject dim) {
                        input.setSubjectName(dim.getString("SUBJECT_NAME"));
                        input.setCategoryId(dim.getString("CATEGORY_ID"));
                    }
                },
                120,
                TimeUnit.SECONDS
        );
        return AsyncDataStream.unorderedWait(
                baseSubjectInfoStream,
                new DimAsyncFunction<TradeCourseOrderBean>() {
                    @Override
                    public String getTable() {
                        return "dim_base_category_info";
                    }

                    @Override
                    public String getId(TradeCourseOrderBean input) {
                        return input.getCategoryId();
                    }

                    @Override
                    public void addDim(TradeCourseOrderBean input, JSONObject dim) {
                        input.setCategoryName(dim.getString("CATEGORY_NAME"));
                    }
                },
                120,
                TimeUnit.SECONDS
        );
    }

    private SingleOutputStreamOperator<TradeCourseOrderBean> windowAndAgg(SingleOutputStreamOperator<TradeCourseOrderBean> beanStream) {
        return beanStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<TradeCourseOrderBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((obj, ts) -> obj.getTs())
                )
                .keyBy(TradeCourseOrderBean::getCourseId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TradeCourseOrderBean>() {
                            @Override
                            public TradeCourseOrderBean reduce(TradeCourseOrderBean value1, TradeCourseOrderBean value2) throws Exception {
                                value1.getOrderIdSet().addAll(value2.getOrderIdSet());
                                value1.getUserIdSet().addAll(value2.getUserIdSet());
                                value1.setOrderAmount(value1.getOrderAmount().add(value2.getOrderAmount()));
                                return value1;
                            }
                        },
                        new ProcessWindowFunction<TradeCourseOrderBean, TradeCourseOrderBean, String, TimeWindow>() {
                            @Override
                            public void process(String s, Context context, Iterable<TradeCourseOrderBean> elements, Collector<TradeCourseOrderBean> out) throws Exception {
                                TradeCourseOrderBean bean = elements.iterator().next();
                                bean.setStt(AtguiguUtil.toDateTime(context.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(context.window().getEnd()));
                                bean.setOrderCount((long) bean.getOrderIdSet().size());
                                bean.setOrderUserCount((long) bean.getUserIdSet().size());
                                bean.setTs(System.currentTimeMillis());
                                out.collect(bean);
                            }
                        });

    }

    private SingleOutputStreamOperator<TradeCourseOrderBean> parseToPojo(DataStreamSource<String> stream) {
        return stream.map(new MapFunction<String, TradeCourseOrderBean>() {
            @Override
            public TradeCourseOrderBean map(String value) throws Exception {
                JSONObject obj = JSON.parseObject(value);
                return TradeCourseOrderBean.builder()
                        .orderIdSet(new HashSet<>(Collections.singletonList(obj.getString("order_id"))))
                        .userIdSet(new HashSet<>(Collections.singletonList(obj.getString("user_id"))))
                        .courseId(obj.getString("course_id"))
                        .courseName(obj.getString("course_name"))
                        .orderAmount(obj.getBigDecimal("final_amount"))
                        .ts(obj.getLong("ts")*1000)
                        .build();
            }
        });
    }
}
